package com.fireline.beauty.seting;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import org.eclipse.wb.swt.SWTResourceManager;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.IDataBaseProvide;
import com.zgw.fireline.base.widgets.BatchItem;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;

/**
 * 床位设置
 * */
public class CwszView implements IFunction {
	/**
	 * @wbp.nonvisual location=17,432
	 */
	private final Dataset cwxxSet = new Dataset("select * from cwsz",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=73,472
	 */
	private final Batch editCw = new Batch(SqliteJdbcImpl.INSTANCE);
	private DataTable dataTable;
	private Composite content;
	private TableCursor tableCursor;
	private Text editorText;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		cwxxSet.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(editCw);
		batchItem.setCommand("delete from cwsz");
		batchItem.setName("删除所有床位信息");
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1,
				1));

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAdd();
			}
		});
		tltmNewItem.setText("添加");

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doRemove();
			}
		});
		toolItem.setText("删除");

		dataTable = new DataTable(content, SWT.BORDER | SWT.FULL_SELECTION,
				cwxxSet);
		dataTable.setLinesVisible(true);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.addListener(SWT.MeasureItem, new Listener() {
			@Override
			public void handleEvent(Event event) {
				event.height = 20;
			}
		});

		tableCursor = new TableCursor(dataTable, SWT.NONE);
		final ControlEditor editor = new ControlEditor(tableCursor);
		editorText = new Text(tableCursor, SWT.BORDER);
		editor.setEditor(editorText);
		editor.grabHorizontal = true;
		editor.grabVertical = true;
		editorText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				TableItem item = tableCursor.getRow();
				item.setText(tableCursor.getColumn(), editorText.getText());
			}
		});
		tableCursor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem item = tableCursor.getRow();
				editorText.setText(item.getText(tableCursor.getColumn()));
				editorText.selectAll();
			}
		});

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "ch");
		dataTableColumn.setText("床号");
		dataTableColumn.setWidth(80);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_1.setText("备注");
		dataTableColumn_1.setWidth(80);

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton_1.setText("保存");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton.setText("关闭");
		setParams();
		createGUIAfter();
		this.content = content;
		return content;
	}

	protected void doRemove() {
		if (dataTable.getSelectionIndex() != -1) {
			dataTable.getSelection()[0].dispose();
		} else {
			MessageDialog
					.openInformation(content.getShell(), "提示", "请选择需删除的记录");
		}
	}

	protected void doAdd() {
		TableItem item = new TableItem(dataTable, SWT.NONE);
		tableCursor.setSelection(item, 0);
		editorText.setText("");
		editorText.setFocus();
		dataTable.setSelection(item);
	}

	protected void doSave() {
		for (TableItem item : dataTable.getItems()) {
			BatchItem addCw = new BatchItem(editCw);
			addCw.setCommand("insert into cwsz (ch,bz,fd) values(?,?,?)");
			addCw.setParam(0, item.getText(0));
			addCw.setParam(1, item.getText(1));
			addCw.setParam(2, "01");
		}
		editCw.execute();
		content.dispose();
	}

	protected void setParams() {
	}

	// 测试方法
	public static void main(String[] args) {
		CwszView cwsz = new CwszView();
		FunctionDialog dlg = new FunctionDialog(new Shell(), cwsz);
		dlg.open();

	}

	protected void createGUIAfter() {
		cwxxSet.update();
	}
}
